#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>

#define DEFAULTSOCKET "/tmp/DEFAULT.sock"//socket id. TREBUIE SA FIE ACELASI CA SI IN SERVER

//Clientul local se gestioneaza din lina de comanda, prin introducerea intrebarilor si a raspunsurilor

/*
ema: trimite o comanda serverului de administrare
*/
void write_text (int socket_fd, const char* text)
{
  int length = strlen (text) + 1;
  write (socket_fd, &length, sizeof (length));//intai trimite dimensiunea
 
  write (socket_fd, text, length);//apoi trimite comanda(intr + rasp)
}

int main (void)
{
  const char* const socket_name = DEFAULTSOCKET;
  char intr[500],r1[300],r2[300],r3[300],rasp[10];
  char* message = malloc(1000);
 /* const char* const message = "regintr#intr=Intrebare1&r1=rasp1&r2=rasp2&r3=rasp3&corect=101\0";*/
  int socket_fd;
  struct sockaddr_un name;
  
  printf("Introduceti intrebarea: ");
  gets(intr);
  printf("Introduceti rapunsul 1: ");
  gets(r1);
  printf("Introduceti rapunsul 2: ");
  gets(r2);
  printf("Introduceti rapunsul 3: ");
  gets(r3);
  printf("Introduceti rapunsurile corecte (1 pt. corect, 0 incorect (de forma 100,10,1)): ");
  scanf("%s",&rasp);
  strcpy(message,"regintr#intr=");
  strcat(message,intr);
  strcat(message,"&r1=");
  strcat(message,r1);
  strcat(message,"&r2=");
  strcat(message,r2);
  strcat(message,"&r3=");
  strcat(message,r3);
  strcat(message,"&corect=");
  strcat(message,rasp);
  
  
  
  socket_fd = socket (PF_LOCAL, SOCK_STREAM, 0);//se creaza un socket local
  name.sun_family = AF_LOCAL;
  strcpy (name.sun_path, socket_name);
  connect (socket_fd, (struct sockaddr *) &name, SUN_LEN (&name));//se conecteaza la server
 
  write_text (socket_fd, message);//se trimite intrebarea + rasp
  
  close (socket_fd);//se inchide conexiunea
  return 0;
}
